#!/usr/bin/env python
# encoding: utf-8

"""
@author: Cao
@file: PicComparev2.py
@time: 12/6/2017 2:34 PM
"""
import cv2
import numpy as np
from PIL import Image, ImageChops
import os

class PicCompare():
    def __init__(self, sourcePic_path, targetPic_Path, Result_Pic):
        self.sourcePic_path = sourcePic_path
        self.targetPic_Path = targetPic_Path
        self.Result_Pic = Result_Pic

    def Comp(self):
        im1 = cv2.imread(self.sourcePic_path)
        im2 = cv2.imread(self.targetPic_Path)
        differ = cv2.subtract(im1, im2)
        comp = not np.any(differ)

        if comp is True:
            msg = "The images are the same!"
            return msg
        else:
            resultPic = cv2.imwrite(self.Result_Pic + ".png", differ)

            return resultPic

def Count(path):
    ls = os.listdir(path)
    count = 0
    for i in ls:
        if os.path.isfile(os.path.join(path, i)):
            count += 1
    return count

def AdjustSize(infile, base_path, filename, *args):

    im = Image.open(infile)
    (x,y) = im.size     #read image size
    x_s = 800           #define standard width
    y_s = y * x_s / x   #calc height based on standard width
    out = im.resize((x_s,y_s),Image.ANTIALIAS)          #resize image with high-quality
    save_file = base_path + filename + str(i) + '-r' + '.png'
    out.save(save_file)
    os.remove(infile)


if __name__ == '__main__':
    s_path = "D:/test/s/"
    t_path = "D:/test/t/"
    result_path = "D:/test/r/"
    s_filename = 's'
    t_filename = 't'

    if Count(s_path) != Count(t_path):
        print ('The Count of files are not equal, please check it!')
    else:
        if os.path.exists(result_path):
            count = Count(t_path)
            for i in range(1, count + 1):
                s_file = s_path + "s" + str(i) + "-r" + ".png"
                t_file = t_path + "t" + str(i) + "-r" + ".png"
                r_file = result_pat + "r" + str(i) + ".png"
                s_file2 = s_path + "s" + str(i) + "-r" + ".png"
                t_file2 = t_path + "t" + str(i) + "-r" + ".png"
                AdjustSize(s_file, s_path, s_filename, i)
                AdjustSize(t_file, t_path, t_filename, i)
                PicCompare(s_file2, t_file2, r_file).Comp()
        else:
            print "Derectory is not Existing, will create it now, please wait......."
            os.mkdir(result_path)
            count = Count(t_path)
            for i in range(1, count + 1):
                s_file = s_path + "s" + str(i) + ".png"
                t_file = t_path + "t" + str(i) + ".png"
                s_file2 = s_path + "s" + str(i) + "-r" + ".png"
                t_file2 = t_path + "t" + str(i) + "-r" + ".png"
                r_file = result_path + "r" + str(i) + ".png"
                AdjustSize(s_file, s_path, s_filename, i)
                AdjustSize(t_file, t_path, t_filename, i)

                PicCompare(s_file2, t_file2, r_file).Comp()

    print "Job Complete!"
    print "Please see the result in Directory " +"*** " + result_path + " ***"